package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.asset.service.IAssetInfoService;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.FilePath;
import com.boss.salary.service.IFilePathService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/FilePathController")
public class FilePathController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(FilePathController.class);
    @Autowired
    private IFilePathService filePathService;
    @Autowired
    private IAssetInfoService assetInfoService;
    /**
     * 新增
     *
     * @param filePath
     * @return
     */
    @PostMapping("/saveFilePath")
    public ApiResultDTO saveFilePath(@RequestHeader("X-Token") String tokenid,
                                                 @RequestBody FilePath filePath) {
        try {
            UserDTO userDTO = getUser(tokenid);
            filePath.setMofDivCode(userDTO.getProvince());
            filePathService.insertFilePath(filePath);
            return ApiResultDTO.success(null, "保存成功");
        } catch (Exception e) {
            logger.error("新增 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "新增 单位信息表发生异常");
        }
    }
    /**
     * 删除
     *
     * @param filePath
     * @return
     */
    @PostMapping("/deleteFilePath")
    public ApiResultDTO deleteFilePath(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody FilePath filePath) {
        try {
            UserDTO userDTO = getUser(tokenid);
            String mofDivCode = userDTO.getProvince();
            filePathService.deleteFilePath(filePath.getId(), mofDivCode);
            return ApiResultDTO.success(null, "删除成功");
        } catch (Exception e) {
            logger.error("删除 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "删除 单位信息表发生异常");
        }
    }
    /**
     * 修改
     *
     * @param filePath
     * @return
     */
    @PostMapping("/updateFilePath")
    public ApiResultDTO updateFilePath(@RequestHeader("X-Token") String tokenid,
                                                   @RequestBody FilePath filePath) {
        try {
            UserDTO userDTO = getUser(tokenid);
            filePath.setMofDivCode(userDTO.getProvince());
            filePathService.updateFilePath(filePath);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 单位信息表发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改 单位信息表发生异常");
        }
    }
    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryFilePathPage")
    public ApiResultDTO queryFilePathPage(@RequestHeader("X-Token") String tokenid,
                                                  @RequestParam(value = "page") String page,
                                                  @RequestParam(value = "limit") String limit
                                                  ) {

        try {
            UserDTO userDTO = getUser(tokenid);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            PageDTO<FilePath> pageDTO = filePathService.queryFilePath(paginationDTO, userDTO.getProvince());

            return ApiResultDTO.success(pageDTO, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }
}
